from django.contrib import admin

"""
将模型注册到Django自带的管理员平台
"""
import inspect
import sys

from django.contrib import admin

from education import models


class UserModelAdmin(admin.ModelAdmin):
    """用户"""
    list_display = ('username', 'is_certificated', 'sex', 'balance', 'tel', 'point')


class TeacherModelAdmin(admin.ModelAdmin):
    """老师介绍"""
    list_display = ('teaname', 'sex', 'teaintro')


class IdcardModelAdmin(admin.ModelAdmin):
    """身份证"""
    list_display = ('user', 'realname', 'idnum')


class CoursemodelAdmin(admin.ModelAdmin):
    """课程"""
    list_display = ('couname', 'couintro', 'degree', 'learn_times', 'stu_count', 'fav_nums', 'price', 'tag')


class OrderModelAdmin(admin.ModelAdmin):
    """订单"""
    list_display = ('orderid', 'goods_name', 'price', 'make_time', 'is_pay', 'pay_time')


class CollectionModelAdmin(admin.ModelAdmin):
    """收藏"""
    list_display = ('collid', 'user', 'course')


class ChapterModelAdmin(admin.ModelAdmin):
    """课程章节"""
    list_display = ('chapterno', 'vedio_name', 'course')
    ordering = ('chapterno',)


class CommentModelAdmin(admin.ModelAdmin):
    """课程评论"""
    list_display = ('user', 'course', 'content', 'make_time')


class SturecordModelAdmin(admin.ModelAdmin):
    """学习记录"""
    list_display = ('user', 'course', 'is_completed', 'make_time')


# 获取当前模块对应的对象
curr_module = sys.modules[__name__]
# 通过Python反射机制获取models模块中所有的类
cls_members = inspect.getmembers(models, inspect.isclass)
# 循环遍历所有模型类注册对应的模型管理类
for cls_name, cls in cls_members:
    # 根据模型的名字动态获取模型管理类
    model_admin_cls = getattr(curr_module, f'{cls_name}ModelAdmin', None)
    if model_admin_cls:
        admin.site.register(cls, model_admin_cls)
    else:
        admin.site.register(cls)
